package OT.shoufouni.mainGUI;


import OT.shoufouni.R;
import OT.shoufouni.serverThreads.serverLogInQuaryThread;
import OT.shoufouni.serverThreads.serverNewUserQuaryThread;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class enterView extends Activity {
	private static final int RECEIVE_MESSAGE = 0;
	Button		logInButton;
	EditText	passwordBox,emailBox;
	private Handler mHandler = new Handler();
	String email;
	
	public static final String ID_MAIL= "ID_MAIL";
    SharedPreferences prefs;
    
    String password;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entergui);
        logInButton 	= (Button)findViewById(R.id.button_commit);
        passwordBox 	= (EditText)findViewById(R.id.edit_text_password);
        emailBox 	= (EditText)findViewById(R.id.edit_text_name);
    
		logInButton.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View view) {
		    	email = emailBox.getText().toString().trim();
		    	password = passwordBox.getText().toString().trim();
		    	
		    	if (("".equals(email))||("".equals(password))){
		    		Toast.makeText(enterView.this,"Please fill all the parameters"+"\nin order lo log in",Toast.LENGTH_LONG).show();
		    		return;
		    	}
		    	
		    	// try login
		    	Toast.makeText(enterView.this,"Connecting to the server"+"\nPlease wait",Toast.LENGTH_LONG).show();
		    	serverLogInQuaryThread quaryThread = 
  	        	  new serverLogInQuaryThread(enterView.this, email,password);
		    	quaryThread.start(); 
			}
		});
    
    }
    public void responseDisplay(final String text, final int mod) {
    	// adding new user
    	if (1==mod){
    		mHandler.post(new Runnable() {
                public void run() {
                	// the new user was created
                	if ("OK".equals(text)){	
                		Context context = getApplicationContext();
	       		        prefs = PreferenceManager.getDefaultSharedPreferences(context);
	       		        Editor editor1 = prefs.edit();
	       		        editor1.putString(ID_MAIL, email);
	       		        editor1.commit();
	       		    	Intent intent = new Intent(enterView.this, mainGUI.class);
	       				startActivityForResult(intent,1 );
                	}
                	// bad password
                	else if ("BAD".equals(text)){
                		Toast.makeText(enterView.this,"The email is already been used\nPlease try again",Toast.LENGTH_LONG).show();
                	}
                	else{
                		Toast.makeText(enterView.this,"Connecting failed"+"\nPlease try again",Toast.LENGTH_LONG).show();
                	}
                }
    		});
    	}
    	// regular log in connection to server
		else{
			mHandler.post(new Runnable() {
	            public void run() {
	            	if ("BAD".equals(text)){
	            		Toast.makeText(enterView.this,"Connecting failed or bad password"+"\nPlease try again",Toast.LENGTH_LONG).show();
	            	}
	            	// create new user
	            	else if ("NEW".equals(text)){
	            		new AlertDialog.Builder(enterView.this)
	    	            .setMessage("Bad Email\n"+"Press yes to create a new acount")
	    	            .setTitle("Unknown Email")
	    	            .setCancelable(false)
	    	            .setPositiveButton("yes", new DialogInterface.OnClickListener() {
	    	                   public void onClick(DialogInterface dialog, int id) {
	    	                	   	// add connection to the server
	    	                	   Toast.makeText(getBaseContext(),"Connecting to the server"+"\nPlease wait",Toast.LENGTH_LONG).show();
		    	       		    	serverNewUserQuaryThread quaryThread = 
		    	         	        	  new serverNewUserQuaryThread(enterView.this, email,password);
		    	       		    	quaryThread.start();
	    	                   }
	    	               })
	    	            .setNegativeButton("No", new DialogInterface.OnClickListener() {
	    	               public void onClick(DialogInterface dialog, int id) {
	    	                    dialog.cancel();
	    	               }
	    	               })
	    	             .show();  
	            	}
	            	else{
	            		// log in
	            		Context context = getApplicationContext();
	       		        prefs = PreferenceManager.getDefaultSharedPreferences(context);
	       		        Editor editor1 = prefs.edit();
	       		        editor1.putString(ID_MAIL, email);
	       		        editor1.commit();
	                    Intent intent = new Intent(enterView.this, mainGUI.class);
	                    startActivityForResult(intent,RECEIVE_MESSAGE);
	            	}
	             }
	          });
	    }		
    }
    	
    
}